<script setup>
import { ref } from 'vue'
import { useUserStore } from '@/stores/modules/user'
import { WarnTriangleFilled } from '@element-plus/icons-vue'
import router from '@/router'
const userStore = useUserStore()
const userform = ref({
  id: '',
  name: '',
  oldpassword: '',
  newpassword: '',
  comfirmpassword: ''
})
userform.value.id = userStore.user.id
userform.value.name = userStore.user.name

const rules = {
  oldpassword: [
    { required: true, message: '请输入旧密码', trigger: 'blur' },
    { min: 6, max: 16, message: '密码长度在6到16个字符', trigger: 'blur' }
  ],
  newpassword: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 6, max: 16, message: '长度在 6 到 16 个字符', trigger: 'blur' },
    {
      pattern: /^(?![0-9]+$)(?![a-zA-Z]+$)(?!.*\s)[0-9A-Za-z]{6,16}$/,
      message: '密码至少包含字母、数字、特殊字符，不能包含空格，长度在 6 到 16 个字符',
      trigger: 'blur'
    }
  ],
  comfirmpassword: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 6, max: 16, message: '长度在 6 到 16 个字符', trigger: 'blur' },
    {
      pattern: /^(?![0-9]+$)(?![a-zA-Z]+$)(?!.*\s)[0-9A-Za-z]{6,16}$/,
      message: '密码至少包含字母、数字、特殊字符，不能包含空格，长度在 6 到 16 个字符',
      trigger: 'blur'
    },
    {
      validator: (rule, value, callback) => {
        if (value !== userform.value.newpassword) {
          callback(new Error('两次输入的密码不一致!'))
        } else {
          callback()
        }
      },
      trigger: 'blur'
    }
  ]
}
const changePasswordSubmit = async () => {
  await ElMessageBox.confirm(`你确定要修改密码吗?`, '温馨提示', {
    confirmButtonText: '确认',
    cancelButtonText: '取消',
    type: 'warning',
    icon: WarnTriangleFilled,
    draggable: true
  })
  console.log(userform.value)
  if (userform.value.oldpassword === userStore.user.password) {
    await userStore.updatePassword(userform.value)
    ElMessage.success('修改密码成功,请重新登录')
    userStore.removeToken()
    await router.push('/login')
  } else ElMessage.error('原密码错误')
}
</script>
<template>
  <div class="user-pwd">
    <div class="user-pwd-title">个人信息</div>
    <el-form :model="userform" :rules="rules">
      <el-form-item label="账户" prop="name" label-width="15%">
        <el-input
          type="text"
          v-model="userform.name"
          autocomplete="off"
          :disabled="true"
        ></el-input>
      </el-form-item>

      <el-form-item label="原密码" prop="password" label-width="15%">
        <el-input
          type="password"
          v-model="userform.oldpassword"
          autocomplete="off"
          show-password
        ></el-input>
      </el-form-item>

      <el-form-item label="新密码" prop="newpassword" label-width="15%">
        <el-input
          type="password"
          v-model="userform.newpassword"
          autocomplete="off"
          show-password
        ></el-input>
      </el-form-item>

      <el-form-item label="确认密码" prop="comfirmpassword" label-width="15%">
        <el-input
          type="password"
          v-model="userform.comfirmpassword"
          autocomplete="off"
          show-password
        ></el-input>
      </el-form-item>
    </el-form>
    <el-button class="user-pwd-btn" type="primary" @click="changePasswordSubmit()">确 定</el-button>
  </div>
</template>

<style scoped lang="scss">
.user-pwd {
  width: 100%;
  height: 100%;
  padding: 40px;
  background-color: #fbfbea;
  border-radius: 10px;

  .user-pwd-title {
    font-size: 24px;
    font-weight: bold;
    margin-bottom: 40px;
  }

  .el-form-item {
    width: 70%;
    margin-bottom: 30px;
  }

  .user-pwd-btn {
    width: 85px;
    height: 40px;
    margin-top: 20px;
    margin-left: 60%;
    border-radius: 10px;
    color: #fff;
    font-size: 18px;
    font-weight: bold;
    cursor: pointer;
  }
}
</style>
